const app = getApp();
Page({
    data: {
        BoxData: '',
        inputCode:'',
        numVal:1,
        LoadMore:0,
        netErr: 0,
    },
    onLoad(options) {
        if (options.input!=1){
            this.ScavengingAjax({order_id: options.order_id,bonus_sn: options.bonus_sn,user_id: options.user_id});
        }
    },
    /*input*/
    codeinput(e) {
        this.setData({
            inputCode: e.detail.value
        })
    },
    /*调起微信扫描*/
    Scaveng: function () {
        wx.scanCode({
            success: (res) => {
                let url = res.result, link = '/pages/proveOrder/proveOrder';
                if (url.substring(0, link.length) === link) {
                    this.ScavengingAjax(app.util.getUrlCode(url));
                } else {
                    app.util.showModal(false, '提示', '没找到相关信息')
                }
            }
        })
    },
    /*扫码请求*/
    ScavengingAjax({ order_id:order_id, bonus_sn: bonus_sn, user_id: user_id }) {
        app.request.request({
            link: '/api/supplier/bonus_detail_by_sn',
            methodtype: 'GET',
            ting: {
                order_id: order_id,
                bonus_sn: bonus_sn,
                user_id: user_id
            },
            succ: (res) => {
                console.log(res)
                let Boxs = res.data.data, BoxMeta = res.data.message;
                if (res.data.status == 200) {
                    Boxs.thumb = app.request.picAdd + Boxs.thumb;
                    this.setData({
                        BoxData: Boxs,
                        LoadMore:0,
                    })
                } else {
                    app.util.showModal(false, '提示', BoxMeta, () => wx.navigateBack({ delta: 1 }))
                }
            },
            fail: (err) => {
                this.setData({
                    netErr: 1,
                })
            }
        })
    },
    /*确定搜索*/
    bonusSnClick(){
        this.bonusSnAjax(this.data.inputCode);
    },
    /*输入请求*/
    bonusSnAjax(i) {
        app.request.request({
            link: '/api/supplier/bonus_detail_by_sn',
            methodtype: 'POST',
            ting: {
                bonus_sn: i,
            },
            succ: (res) => {
                console.log(res)
                let Boxs = res.data.data, BoxMeta = res.data.message;
                if (res.data.status == 200) {
                    Boxs.thumb = app.request.picAdd + Boxs.thumb;
                    this.setData({
                        BoxData: Boxs.add_time == 0 ? '' : Boxs,
                        LoadMore: Boxs.add_time == 0 ? 7 : 0,
                    })
                } else {
                    app.util.showModal(false, '提示', BoxMeta)
                }
            },
            fail: (err) => {
                app.util.showModal(false, '网络错误', '请检查网络，重新确认核销');
            }
        })
    },
    /*确认核销*/
    confirmClick(e) {
        app.util.showModal(true, '确定核销', '是否确认核销该订单?', () => {
            this.confirmAjax(e.currentTarget.dataset.order_id);
        });
    },
    /*确认核销请求*/
    confirmAjax(i){
        app.request.request({
            link: '/api/supplier/received_order',
            methodtype: 'POST',
            ting: {
                order_id: i,
                done_number: this.data.numVal
            },
            succ: (res) => {
                console.log(res)
                let Boxs = res.data.data, BoxMeta = res.data.message;
                if (res.data.status == 200) {
                    app.util.Success(BoxMeta, () => {
                        this.setData({
                            BoxData: '',
                            numVal: 1,
                        })
                    });
                } else {
                    app.util.showModal(false, '提示', BoxMeta)
                }
            },
            fail: (err) => {
                app.util.showModal(false, '网络错误', '请检查网络，重新确认核销');
            }
        })
    },
    /*加一减一*/
    addredClick(e) {
        let num = Number(this.data.numVal);
        if (e.currentTarget.dataset.inx == 0) {
            num--;
        } else {
            num++;
        }
        this.setData({
            numVal: num < 1 ? 1 : num > this.data.BoxData.goods_number ? this.data.BoxData.goods_number : num,
        })
    },
    /*网络报错*/
    ErrorF5() {
        this.setData({
            netErr: 0,
        })
    },
    
})